#ifndef __AUDIO_ENCODE__H__
#define __AUDIO_ENCODE__H__

#include <map>
#include <mutex>
#include <functional>
#include "Pal/AudioEnc.h"
#include "frame_common.h"
#include "Thread/Thread.h"

namespace Media{

class AudioEncode{
public:
	using CallBack = std::function<void(const FrameCommon::Frame::Ptr &)>;

	static AudioEncode *instance(int channel);

	bool setFormat(AudioEncFormat* format);

	bool start(void *thiz, const FrameCommon::frame_callback_func &callback);

	bool stop(void *thiz);

	bool getFormat(AudioEncFormat *format);

	bool start();

	bool stop();

	bool getPacket(char *buffer, int* length);
	bool getPacket(char *buffer, int* length, uint64& pts);

	bool getSpecificInfo(char **ppBuffer, unsigned long *pSize);
	
private:
	AudioEncode(int channel);

	bool createEncoder();

	bool Init();

	void ThreadProc(Fdt::Thread *thread);
private:
	static std::map<int, AudioEncode *> 	s_encoder_map;

	uint64_t								_start_time = 0;
	uint64_t								_pts = 0;
	//start作为启动标志,避免多个业务多次启停
	bool									_started = false;
	//_run作为运行标志,某些使能情况下不能去获取帧,例如设置格式重更新启停的时候
	AudioEncoder *							_audioEncoder = nullptr;
	int 									_channel = 0;
	std::recursive_mutex 					_mutex;
	std::map<void *, std::shared_ptr<FrameCommon::frame_callback_func>> 	_func_map;

	AudioEncFormat                          _encFormat;

	AudioEncPacket                          _encpPacket;
	
	Fdt::Thread 						    _thread{"Audio0"};//thread放在最后,保证thread最后构造,最先析构
};

}//namespace Media
#endif

